﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Xml;


public partial class _Default : System.Web.UI.Page
{
    private DataTable m_DataTable = new DataTable();

    private double m_x;
    private double m_y;

    /* change string to double (9 septembre 2012) */
		
		protected void Page_Load(object sender, EventArgs e)
    {
        double x;
        double y;

        m_DataTable.Columns.Add("Title");
        m_DataTable.Columns.Add("x");
        m_DataTable.Columns.Add("y");

        String address1 = "121 place Georges Pompidou, Paris";
        Coord coord = GoogleMaps.getLatLng(address1);

        m_DataTable.Rows.Add("121 place Georges Pompidou, Paris", coord.lat, coord.lng);
        m_DataTable.Rows.Add("7 rue de Turbigo, Paris", 48.863599, 2.34783);
        m_DataTable.Rows.Add("50 rue de Rivoli, Paris", 48.857066, 2.354134);
        
        String TextBox1ClientId = TextBox1.ClientID;
        String DropdownClientId = DropDownList1.ClientID;
        DropDownList1.Attributes.Add("onchange", "javascript:" + TextBox1ClientId + ".value=" + DropdownClientId + ".value");

        if (!IsPostBack) {

            int nTotalRecords = m_DataTable.Rows.Count;
            for (int i = 0; i < nTotalRecords; i++) {
                DropDownList1.Items.Add(m_DataTable.Rows[i]["Title"].ToString());
            }

            TextBox1.Text = DropDownList1.SelectedItem.Text;
            String title = m_DataTable.Rows[0]["Title"].ToString();

            DataRow[] dataRowArray = m_DataTable.Select("[Title]='"+ title + "'");
            m_x = Convert.ToDouble(dataRowArray[0]["x"]);
            m_y = Convert.ToDouble(dataRowArray[0]["y"]);

            x = m_x;
            y = m_y;

            GoogleMaps.createMap(this,x, y, title);

        }
    }

    /* fix crash when entering new address (9 septembre 2012) */
		/* change string to double (9 septembre 2012) */
		protected void ButtonGo_Click(object sender, EventArgs e)
    {

        double x;
        double y;


        String address1 = HttpUtility.JavaScriptStringEncode(TextBox1.Text);
        Coord coord = GoogleMaps.getLatLng(address1);


        try
        {


            DataRow[] dataRowArray = m_DataTable.Select("[Title]='" + address1 + "'");
            m_x = Convert.ToDouble(dataRowArray[0]["x"]);
            m_y = Convert.ToDouble(dataRowArray[0]["y"]);

            x = m_x;
            y = m_y;

        }
        catch
        {

            x = coord.lat;
            y = coord.lng;

        }

        GoogleMaps.createMap(this, x, y, address1);        
    }

    protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        TextBox1.Text = DropDownList1.SelectedItem.Text;
    }
}
